﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TeamTime.Classes;
using StillVoiceCommon;

namespace TeamTime
{
    public partial class TimeEntryFormControl : System.Web.UI.UserControl
    {
        public string SaveButtonText { get { return this.btnSave.Text; } set { this.btnSave.Text = value; } }

        public bool SaveButtonVisible { get; set; }
        //private bool liteMode;


        public bool SuccessfullySaved
        {
            get { return this.chkSuccessfullySaved.Checked; }
            set { this.chkSuccessfullySaved.Checked = value; }
        }
        public Int32 CompanyID
        {
            get
            {
                if (this.txtCompanyID.Text == "") this.txtCompanyID.Text = "0";
                return Convert.ToInt32(this.txtCompanyID.Text);
            }
            set
            {
                this.txtCompanyID.Text = value.ToString();
            }
        }
        public string TaskName
        {
            set { setForAdding(value); }
        }
        public Int32 TaskID
        {
            //get { return Convert.ToInt32(this.txtID.Text); }
            set
            {
                Task task = new Task();
                task.load(value.ToString());
                this.setForAdding(task.CompanyProjectTask);
            }
        }
        public Int32 TimeEntryID
        {
            get { return Convert.ToInt32(this.txtID.Text); }
            set
            {
                LoadDetailsForm(Convert.ToInt32(value));
                //                this.setForAdding(task.CompanyProjectTask);
            }
        }


        public event System.EventHandler SaveClicked;
        private void TimeEntryFormControl_SaveClicked(object sender, EventArgs e) { }
        public delegate void SaveClickEventHandler(object sender, EventArgs e);

        public event System.EventHandler CopyClick;
        private void TimeEntryFormControl_CopyClick(object sender, EventArgs e) { }
        public delegate void CopyClickEventHandler(object sender, EventArgs e);

        public event System.EventHandler LoadDupClicked;
        private void TimeEntryFormControl_LoadDupClicked(object sender, EventArgs e) { }
        public delegate void LoadDupClickEventHandler(object sender, EventArgs e);

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {

                StaffMembers staffMembers = new StaffMembers();
                Functions.dropDownListPopulate(this.ddlStaffMemberID, staffMembers.getStaffMembers(false), "ID", "FullName");

                EnableControls();
            }
            Users user = new Users();
            Int32 PersonID = user.getPersonIDFromUser(Session["UserID"].ToString());
            ddlStaffMemberID.SelectedValue = Convert.ToString(user.getIDFromViewStaffMembers(PersonID));
        }
        public void EnableControls()
        {
            Functions.enableChildControls(this.pnlDetails);
            this.txtUserNameModifiedBy.Enabled = true;
            this.txtRecordDate.Enabled = true;
            this.trID.Visible = (txtID.Text != "");
            this.txtID.Enabled = false;
            this.txtDuration.Enabled = false;
            this.btnCopy.Visible = (txtID.Text != "");
        }
        public void setForAdding(string TaskName)
        {
            setForAdding();
            this.txtTaskName.Text = TaskName;
        }
        public void setForAdding()
        {
            Functions.clearChildcontrols(this.pnlDetails);
            this.calStartDate.Text = DateTime.Now.ToString("yyyy/MM/dd");
            this.calEndDate.Text = DateTime.Now.ToString("yyyy/MM/dd");
            StaffMembers staffMembers = new StaffMembers();
            Functions.dropDownListPopulate(this.ddlStaffMemberID, staffMembers.getStaffMembers(false), "ID", "FullName");
            Users user = new Users();
            Int32 PersonID = user.getPersonIDFromUser(Session["UserID"].ToString());
            ddlStaffMemberID.SelectedValue = Convert.ToString(user.getIDFromViewStaffMembers(PersonID));
            EnableControls();
        }
        private void LoadDetailsForm(Int32 ID)
        {
            TimeEntry timeEntry = new TimeEntry();
            timeEntry.load(ID.ToString());
            Tasks tasks = new Tasks();
            // Functions.dropDownListPopulate(this.ddlTaskID, tasks.getTasks(false, false), "ID", "CompanyProjectTask");
            Functions.populateFormControlsFromDataTableFields(timeEntry, this.pnlDetails);
            this.txtTaskName.Text = timeEntry.CompanyProjectTask;
            EnableControls();
            //if (TimeEntry.Fields["CompletionDate"] == DBNull.Value)
            //    ddlTaskID.Enabled = false;

        }
        public void btnSave_Click(object sender, EventArgs e)
        {
            SuccessfullySaved = false;
            TimeEntries TimeEntries = new TimeEntries();

            TimeEntry timeEntry = new TimeEntry();

            if (this.txtID.Text == "") { timeEntry.setForAdd(); } else { timeEntry.load(this.txtID.Text); }

            DateTime startdate = Convert.ToDateTime(this.calStartDate.Text);
            DateTime enddate = Convert.ToDateTime(this.calEndDate.Text);
            if (enddate > startdate)
            {
                lbldate.ForeColor = System.Drawing.Color.Red;
                lbldate.Text = "End must be > start";


            }
            else
            {
                lbldate.Visible = false;

                this.lblTaskValid.Text = "";
                TimeEntries timeEntries = new TimeEntries();
                Int32 taskID = timeEntries.getTaskIDFromDescription(this.txtTaskName.Text);
                if (taskID < 1)
                {
                    this.lblTaskValid.Text = "Please enter a valid task";
                }
                else
                {
                    timeEntry.Fields["TaskID"] = taskID;
                    timeEntry.Fields["StaffMemberID"] = this.ddlStaffMemberID.SelectedValue;
                    if (this.calStartDate.Text != "") timeEntry.Fields["StartDate"] = this.calStartDate.Text; else timeEntry.Fields["StartDate"] = DBNull.Value;
                    timeEntry.Fields["StartTime"] = this.txtStartTime.Text;
                    if (this.calEndDate.Text != "") timeEntry.Fields["EndDate"] = this.calEndDate.Text; else timeEntry.Fields["EndDate"] = DBNull.Value;

                    timeEntry.Fields["EndTime"] = this.txtEndTime.Text;
                    DateTime start = Convert.ToDateTime(this.calStartDate.Text + " " + this.txtStartTime.Text);
                    DateTime end = Convert.ToDateTime(this.calEndDate.Text + " " + this.txtEndTime.Text);

                    timeEntry.Fields["Duration"] = end.Subtract(start).TotalMinutes / 60;
                    timeEntry.Fields["TimeEntryDescription"] = this.txtTimeEntryDescription.Text;
                    timeEntry.UserIDModifiedBy = Convert.ToInt32(Session["UserID"]);
                    timeEntry.updateWithUserIDDate(); //Updates RecordDate
                    this.txtID.Text = timeEntry.KeyValue;
                    timeEntry = null;
                    SuccessfullySaved = true;
                    lblSaveMessage.ShowStatusMessage("Successfully saved");
                    LoadDetailsForm(Convert.ToInt32(this.txtID.Text));

                }
            }

            EnableControls();
            if (this.SaveClicked != null)
                this.SaveClicked(sender, e);

        }
        public void btnCopy_Click(object sender, EventArgs e)
        {

            txtID.Text = "";
            if (txtEndTime.Text != "")
            {
                txtStartTime.Text = txtEndTime.Text;
                this.txtTimeEntryDescription.Text = "";
                this.txtDuration.Text = "";
            }
            txtEndTime.Text = "";
            if (this.SaveClicked != null)
                this.SaveClicked(sender, e);

        }


        //protected void lnkLoadDup_Click(object sender, EventArgs e)
        //{
        //    if (this.lnkLoadDup.Attributes["DupID"] != "")
        //    {
        //        LoadDetailsForm(Convert.ToInt32(this.lnkLoadDup.Attributes["DupID"]));
        //        this.lnkLoadDup.Visible = false;
        //    }

        //    if (this.LoadDupClicked != null)
        //        this.LoadDupClicked(sender, e);
        //}

        //private void Save()
        //{


        //}

    }
}